<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="cn.estore.entity.Product" %>
<%@ page import="cn.estore.dao.ProductDao" %>
<%@ page import="java.sql.SQLException" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>商品信息修改结果</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f8f9fa;
			margin: 0;
			padding: 0;
			text-align: center;
		}

		main {
			padding: 20px;
		}

		p.success {
			color: green;
			font-size: 18px;
			margin-top: 20px;
		}

		p.failure {
			color: red;
			font-size: 18px;
			margin-top: 20px;
		}
	</style>
</head>
<body>
<main>
	<%
		request.setCharacterEncoding("UTF-8");
		// 获取商品 id 参数并转换为整数，添加异常处理和校验
		int id;
		try {
			String idStr = request.getParameter("id");
			if (idStr == null || idStr.trim().isEmpty()) {
				throw new IllegalArgumentException("商品 id 不能为空");
			}
			id = Integer.parseInt(idStr);
		} catch (IllegalArgumentException e) {
			out.println("<p class='failure'>商品 id 参数错误，请检查输入。</p>");
			return;
		}

		// 获取商品名称参数并校验非空
		String name = request.getParameter("name");
		if (name == null || name.trim().isEmpty()) {
			out.println("<p class='failure'>商品名称不能为空。</p>");
			return;
		}

		// 获取商品产地参数并校验非空
		String producingArea = request.getParameter("producingArea");
		if (producingArea == null || producingArea.trim().isEmpty()) {
			out.println("<p class='failure'>商品产地不能为空。</p>");
			return;
		}

		// 获取商品单价参数并转换为 double，添加异常处理和校验
		double marketPrice;
		try {
			String marketPriceStr = request.getParameter("marketPrice");
			if (marketPriceStr == null || marketPriceStr.trim().isEmpty()) {
				throw new IllegalArgumentException("商品单价不能为空");
			}
			marketPrice = Double.parseDouble(marketPriceStr);
			if (marketPrice <= 0) {
				throw new IllegalArgumentException("商品单价必须大于 0");
			}
		} catch (IllegalArgumentException e) {
			out.println("<p class='failure'>商品单价参数错误，请检查输入。</p>");
			return;
		}

		// 获取商品简介参数并校验非空
		String description = request.getParameter("description");
		if (description == null || description.trim().isEmpty()) {
			out.println("<p class='failure'>商品简介不能为空。</p>");
			return;
		}

		Product e = new Product();
		e.setId(id);
		e.setName(name);
		e.setProducingArea(producingArea);
		e.setMarketPrice(marketPrice);
		e.setDescription(description);

		ProductDao dao = new ProductDao();
		boolean f = false;
		// 调用数据库更新方法
		f = dao.updateProduct(e);

		if (f) {
			out.println("<p class='success'>修改成功！修改后的商品信息：名称 - " + name + "，产地 - " + producingArea + "，单价 - " + marketPrice + "，简介 - " + description + "</p>");
		} else {
			out.println("<p class='failure'>修改失败！</p>");
		}
	%>
</main>
</body>
</html>
